/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cmcc.zysoft.infocollection.common.BaseController;
import com.cmcc.zysoft.infocollection.model.Company;
import com.cmcc.zysoft.infocollection.service.CompanyPcService;
import com.cmcc.zysoft.spring.security.model.User;
import com.cmcc.zysoft.spring.security.util.SecurityContextUtil;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: CompanyController.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-9-4 下午3:07:39
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Controller
@RequestMapping("/pc/company")
public class CompanyController extends BaseController {
	
	/**
	 * 属性名称: companyPcService.
	 * 类       型: CompanyPcService.
	 */
	@Resource
	private CompanyPcService companyPcService;
	
	/**
	 * 企业列表页面.
	 * @return String
	 */
	@RequestMapping("/main.htm")
	public String main() {
		return "/pc/company/list";
	}
	
	/**
	 * 获取企业列表信息.
	 * @return List<Map<String, Object>>
	 */
	@RequestMapping("/tree.htm")
	@ResponseBody
	public List<Map<String, Object>> tree() {
		return this.companyPcService.companyTree();
	}
	
	/**
	 * 根据企业名称搜索企业.
	 * @param companyName 企业名称
	 * @return List<Map<String, Object>>
	 */
	@RequestMapping("/search.htm")
	@ResponseBody
	public List<Map<String, Object>> search(String companyName) {
		return this.companyPcService.search(companyName);
	}
	
	/**
	 * 添加企业页面.
	 * @return String
	 */
	@RequestMapping("/add.htm")
	public String add() {
		return "/pc/company/add";
	}
	
	/**
	 * 添加企业.
	 * @param company 企业信息
	 * @return String
	 */
	@RequestMapping("/addCompany.htm")
	@ResponseBody
	@Transactional
	public String addCompany(Company company) {
		User user = SecurityContextUtil.getCurrentUser();
		return this.companyPcService.addCompany(company, user.getUserId());
	}
	
	/**
	 * 编辑企业信息页面.
	 * @param modelMap modelMap
	 * @param companyId 企业ID
	 * @return String
	 */
	@RequestMapping("/edit.htm")
	public String edit(ModelMap modelMap, String companyId) {
		Company company = this.companyPcService.getEntity(companyId);
		modelMap.addAttribute("company", company);
		return "/pc/company/edit";
	}
	/**
	 * 更新企业信息.
	 * @param company 企业信息
	 * @return String
	 */
	@RequestMapping("/updateCompany.htm")
	@ResponseBody
	@Transactional
	public String updateCompany(Company company) {
		User user = SecurityContextUtil.getCurrentUser();
		return this.companyPcService.updateCompany(company, user.getUserId());
	}
	
	/**
	 * 检查企业代码是否已存在.
	 * @param companyId 企业ID
	 * @param companyCode 企业代码
	 * @return map
	 */
	@RequestMapping("/checkCompanyCode.htm")
	@ResponseBody
	public Map<String, Object> checkCompanyCode(String companyId, String companyCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		Boolean isExist = this.companyPcService.checkCompanyCode(companyId, companyCode);
		if(isExist) {
			map.put("code", "0");
		} else {
			map.put("code", "1");
		}
		return map;
	}
	
	/**
	 * 检查企业名称是否已存在.
	 * @param companyId 企业ID
	 * @param companyName 企业名称
	 * @return map
	 */
	@RequestMapping("/checkCompanyName.htm")
	@ResponseBody
	public Map<String, Object> checkCompanyName(String companyId, String companyName) {
		Map<String, Object> map = new HashMap<String, Object>();
		Boolean isExist = this.companyPcService.checkCompanyName(companyId, companyName);
		if(isExist) {
			map.put("code", "0");
		} else {
			map.put("code", "1");
		}
		return map;
	}
	
	/**
	 * 删除企业.
	 * @param companyIds 企业ID集合
	 * @return String
	 */
	@RequestMapping("/delete.htm")
	@ResponseBody
	@Transactional
	public String delete(String companyIds) {
		String[] ids = companyIds.split(",");
		for(int i = 0; i < ids.length; i++) {
			Company company = this.companyPcService.getEntity(ids[i]);
			company.setDelFlag("1");
			this.companyPcService.updateEntity(company);
		}
		return "SUCCESS";
	}
	
	/**
	 * 获取企业树-用户增加或修改企业.
	 * @return List<Map<String, Object>>
	 */
	@RequestMapping("/combotree.htm")
	@ResponseBody
	public List<Map<String, Object>> combotree() {
		List<Map<String, Object>> list = this.companyPcService.companyTree();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", "0");
		map.put("text", "无");
		list.add(map);
		return list;
	}
	
	/**
	 * 获取企业树-用户增加或修改部门信息.
	 * @return List<Map<String, Object>>
	 */
	@RequestMapping("/companyTree.htm")
	@ResponseBody
	public List<Map<String, Object>> companyTree() {
		return this.companyPcService.companyTree();
	}
	
	/**
	 * 异步加载企业部门树-员工管理.
	 * @param id 可能是公司c_id,部门d_id,加前缀区分
	 * @return List<Map<String, Object>>
	 */
	@RequestMapping("/companyDeptTree.htm")
	@ResponseBody
	public List<Map<String, Object>> companyDeptTree(String id) {
		return this.companyPcService.companyDeptTree(id);
	}
}